import Vue from 'vue'
import Router from 'vue-router'
// 引入home
import Home from './views/Home.vue'
// 引入login
import Login from './views/login.vue'
// 引入welcome
import Welcome from './views/welcom.vue'
// 引入user
import Users from './views/users.vue'
// 引入cat页面
import Cat from './views/cat.vue'
// 引入param
import Param from './views/param.vue'
// 引入goods
import Goods from './views/goods.vue'
// 引入goodsadd
import Goodsadd from './views/goodsadd.vue'
// 引入report
import Report from './views/report.vue'

Vue.use(Router)

var router = new Router({
  routes: [
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      children: [
        { path: '/', redirect: '/welcome' },
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/categories', component: Cat },
        { path: '/params', component: Param },
        { path: '/goodsadd', component: Goodsadd },
        { path: '/goods', component: Goods },
        { path: '/reports', component: Report }
      ]
    }
  ]
})
// 这是路由守卫
router.beforeEach((to, from, next) => {
  var token = window.sessionStorage.getItem('token')
  // 判断如果没有携带token 并且要去的不是登录页面 就让他跳转到登录页面
  if (!token && to.path !== '/login') {
    return next('/login')
  }
  next()
})

export default router
